package com.yugao.fintech.draper.message.queue.config;

import com.hazelcast.core.HazelcastInstance;
import com.yugao.fintech.draper.message.queue.client.HazelcastMQTemplate;
import com.yugao.fintech.draper.message.queue.interceptor.HazelcastMessageInterceptor;
import com.yugao.fintech.draper.message.queue.support.HazelcastMessageListenerProvider;
import com.yugao.fintech.draper.message.queue.support.broadcast.AbstractHazelcastBroadcastMessageListener;
import com.yugao.fintech.draper.message.queue.support.group.AbstractHazelcastGroupMessageListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.List;

@AutoConfiguration
public class HazelcastMessageAutoConfiguration {
    @Bean
    HazelcastMQTemplate hazelcastMQTemplate(@Qualifier("hazelcastInstance") HazelcastInstance hazelcastInstance,
                                            List<HazelcastMessageInterceptor> interceptors) {
        return new HazelcastMQTemplate(hazelcastInstance, interceptors);
    }

    @Bean
    HazelcastMessageListenerProvider hazelcastMessageListenerProvider(HazelcastMQTemplate hazelcastMQTemplate,
                                                                      @Qualifier("hazelcastInstance") HazelcastInstance hazelcastInstance,
                                                                      List<AbstractHazelcastBroadcastMessageListener<?>> topicMessageListeners,
                                                                      List<AbstractHazelcastGroupMessageListener<?>> groupMessageListeners) {
        HazelcastMessageListenerProvider hazelcastMessageListenerProvider = new HazelcastMessageListenerProvider(hazelcastInstance, hazelcastMQTemplate);
        topicMessageListeners.forEach(hazelcastMessageListenerProvider::register);
        groupMessageListeners.forEach(hazelcastMessageListenerProvider::register);
        return hazelcastMessageListenerProvider;
    }
}
